/***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
**Code for replicating Figures 2, 3 and 4: EVENT Study Parallel trends:

*For detailed information on the variables and statistical methods, refer to the "Read Me-Phase-II Tests" file

to run the test or proportional hazards, run the following code after each run of a hazard test:
		estat phtest
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************/
use "pseudo_phase2_main",replace

**define the hazard function:
stset phase2age, id(dp) failure(dp_grad==1)

/*****************************
	code for the parallel trends:
*****************************/

***identify the shock type: btdshock (for Figure 2) SMST shock (for Figure 3) SM shock (for Figure 4)
foreach shock in btdshock smst sm{

	***identify the sample: all (panel A of each figure), low competition (panel B of each figure) or high competition (panel C of each figure)
	foreach sample in all lowcomp hicomp{

		stcox   `shock'_yn5 `shock'_yn4 `shock'_yn3 `shock'_yn2 `shock'_yn1  `shock'_yp1 `shock'_yp2 `shock'_yp3 `shock'_yp4 `shock'_yp5 i.fqdate if `sample'==1 ,strat(i3) vce(cluster dp)

		**plotting the parallel trends
		coefplot,keep(`shock'_yn5   `shock'_yn4 `shock'_yn3 `shock'_yn2 `shock'_yn1  `shock'_yp1 `shock'_yp2 `shock'_yp3 `shock'_yp4 `shock'_yp5) rename(`shock'_yn5=t-5 `shock'_yn4=t-4  `shock'_yn3=t-3  `shock'_yn2=t-2  `shock'_yn1=t-1  `shock'_yp1=t+1 `shock'_yp2=t+2 `shock'_yp3=t+3 `shock'_yp4=t+4 `shock'_yp5=t+5) yline(0) vert  ytitle(magnitude)  ciopts(recast(rcap))

	}

}
	
	
/***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
**Code for replicating Table 6:

*For detailed information on the variables and statistical methods, refer to the "Read Me-Phase-II Tests" file

to run the test or proportional hazards, run the following code after each run of a hazard test:
		estat phtest

***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************/
		
use "pseudo_phase2_main",replace
**define the hazard function:
stset phase2age, id(dp) failure(dp_grad==1)

**define a variable for the sample in the first four columns of both Panels: anyexp:
g anyexp=1
		
***************************************************************************
/*****************************
*Panel A: BTD Shock as the Independent Variable
*****************************/
***************************************************************************
***identify the exposure sample: anyexp (columns 1-4), low exposure (columns 5-8), hiexposure (columns 9-12)
foreach exp in anyexp lowexp hiexp{
	
		stcox c.btdshock i.fqdate if `exp'==1 ,strat(i3 ) cluster(dp)
		outreg2 using table6.xls, append dec(3) tstat ctitle(`exp'_anycomp) keep(c.btdshock) 
		*******full sample with competition interaction
		stcox c.btdshock##c.lncomp i.fqdate if `exp'==1,strat(i3 ) cluster(dp)
		outreg2 using table6.xls, append dec(3) tstat ctitle(`exp'_anycomp) keep(c.btdshock##c.lncomp) 
		
			/*****************************
			*low and high comp:
			*****************************/
			**identify the competition subsample:
			
			foreach comp in lowcomp hicomp{
			stcox c.btdshock i.fqdate  if  `comp'==1 & `exp'==1,strat(i3 ) cluster(dp)
			outreg2 using table6.xls, append dec(3) tstat ctitle(`exp'_`comp' ) keep(c.btdshock) 
			
			}
}

***************************************************************************
/*****************************
*Panel B: sm and smst as the Independent Variables
*****************************/
***************************************************************************
***identify the exposure sample: anyexp (columns 1-4), low exposure (columns 5-8), hiexposure (columns 9-12)
foreach exp in anyexp lowexp hiexp{
	
		stcox c.smst c.sm i.fqdate if `exp'==1 ,strat(i3 ) cluster(dp)
		outreg2 using table6.xls, append dec(3) tstat ctitle(`exp'_anycomp) keep(c.smst c.sm) 
		*******full sample with competition interaction
		stcox c.smst##c.lncomp c.sm##c.lncomp i.fqdate if `exp'==1,strat(i3 ) cluster(dp)
		outreg2 using table6.xls, append dec(3) tstat ctitle(`exp'_anycomp) keep(c.smst##c.lncomp c.sm##c.lncomp) 
		
			/*****************************
			*low and high comp:
			*****************************/
			**identify the competition subsample:
			
			foreach comp in lowcomp hicomp{
			stcox c.smst c.sm i.fqdate  if  `comp'==1 & `exp'==1,strat(i3 ) cluster(dp)
			outreg2 using table6.xls, append dec(3) tstat ctitle(`exp'_`comp' ) keep(c.smst c.sm) 
			
			}
}	

/***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
**Code for replicating Table 7:

*For detailed information on the variables and statistical methods, refer to the "Read Me-Phase-II Tests" file

to run the test or proportional hazards, run the following code after each run of a hazard test:
		estat phtest

***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************/
		
use "pseudo_phase2_exantelevel",replace

**define the hazard function:
stset phase2age, id(dp) failure(dp_grad==1)
		
***************************************************************************
/*****************************
*Panel A: BTD Shock as the Independent Variable
*****************************/
***************************************************************************
	
		stcox c.btdshock i.fqdate  ,strat(i3 ) cluster(dp)
		outreg2 using table7.xls, append dec(3) tstat ctitle(anycomp) keep(c.btdshock) 
		*******full sample with competition interaction
		stcox c.btdshock##c.lncomp i.fqdate ,strat(i3 ) cluster(dp)
		outreg2 using table7.xls, append dec(3) tstat ctitle(anycomp) keep(c.btdshock##c.lncomp) 
		
			/*****************************
			*low and high comp:
			*****************************/
			**identify the competition subsample:
			
			foreach comp in lowcomp hicomp{
			stcox c.btdshock i.fqdate  if  `comp'==1 ,strat(i3 ) cluster(dp)
			outreg2 using table7.xls, append dec(3) tstat ctitle(`comp' ) keep(c.btdshock) 
			
			}

***************************************************************************
/*****************************
*Panel B: sm and smst as the Independent Variables
*****************************/
***************************************************************************
	
		stcox c.smst c.sm i.fqdate ,strat(i3 ) cluster(dp)
		outreg2 using table7.xls, append dec(3) tstat ctitle(anycomp) keep(c.smst c.sm) 
		*******full sample with competition interaction
		stcox c.smst##c.lncomp c.sm##c.lncomp i.fqdate ,strat(i3 ) cluster(dp)
		outreg2 using table7.xls, append dec(3) tstat ctitle(anycomp) keep(c.smst##c.lncomp c.sm##c.lncomp) 
		
			/*****************************
			*low and high comp:
			*****************************/
			**identify the competition subsample:
			
			foreach comp in lowcomp hicomp{
			stcox c.smst c.sm i.fqdate  if  `comp'==1 ,strat(i3 ) cluster(dp)
			outreg2 using table7.xls, append dec(3) tstat ctitle(`comp' ) keep(c.smst c.sm) 
			
			}
			
/***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************
**Code for replicating Table 8 Panel A:

*For detailed information on the variables and statistical methods, refer to the "Read Me-Phase-II Tests" file

to run the test or proportional hazards, run the following code after each run of a hazard test:
		estat phtest

***************************************************************************
***************************************************************************
***************************************************************************
***************************************************************************/

use "pseudo_phase2_nevershocked",replace
***stset:
stset phase2age, id(dp) failure(dp_grad==1)
		/*****************************
		*hazard regression
		*****************************/
		
		**identify the subsample conditional on focal mkt competition: low comp (first two columns), and high comp (last two columns)
foreach comp in lowcomp hicomp{
		stcox hicomp_hiexp_rival   if `comp'==1  ,strat(i3)  cluster(dp)
			outreg2 using table8.xls, append dec(3) tstat ctitle(`comp' ) keep(hicomp_hiexp_rival) 
		**hi comp
		stcox hicomp_hiexp_rival hicomp_rival hiexp_rival if `comp'==1 ,strat(i3)  cluster(dp)
		outreg2 using table8.xls, append dec(3) tstat ctitle(`comp' ) keep(hicomp_hiexp_rival hicomp_rival hiexp_rival) 
}